package com.zhaopin.gitlab;

import com.zhaopin.utils.FileUtil;
import com.zhaopin.utils.GitUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

@Slf4j
public class JustCheckExpSep {


    //职位推荐名称
    static String projectName = "project";
    //职位搜索名称
//    static String projectName = "position";
    static String outputDir = projectName.equals("project") ? "dir/gitlab/recommend" : "dir/gitlab/position";
    static String RPOJECT_NAME_FILE = "dir/gitlab/"+projectName+".txt";
    static String gitFile = "/Users/zhaopin/IdeaProjects/"+projectName+"/%s";
    static String inPath = "/Users/zhaopin/IdeaProjects/"+projectName+"/%s/project/src/main/java/config";



    static String old = "\"CV_DESIRED_SALARY\"\n" +
            "\"CV_DESIRED_SALARY_MAX\"\n" +
            "\"CV_CURRENT_JOBTYPE_NEW\"\n" +
            "\"CV_DESIRED_CITY_ALL\"\n" +
            "\"CV_DESIRED_JOBTYPE\"\n" +
            "\"CV_DESIRED_EMPLOYMENT_TYPE\"\n" +
            "\"CV_WORK_CITY\"\n" +
            "\"CV_DESIRED_SALARY_MIN\"\n" +
            "\"CV_CURRENT_INDUSTRY_NEW\"\n" +
            "\"CV_FULL_INDEX_TOP\"\n" +
            "\"CV_DESIRED_INDUSTRY_NEW\"\n" +
            "\"CV_DESIRED_INDUSTRY\"\n" +
            "\"CV_DESIRED_JOB_TYPE_NEW\"\n" +
            "\"CV_DESIRED_CITY\"\n" +
            "\"CV_DESIRED_JOBTYPE_THIRDLEVEL_\n" +
            "\"CV_DESIRED_JOBTYPE_SECONDLEVEL_";
    static String separator = "\n";
    static String newValue = "\"similarJobLevelForCV_Exp5\"";

    public static void main(String[] args) throws IOException {

        List<String> projectNameList = FileUtils.readLines(new File(RPOJECT_NAME_FILE), Charset.defaultCharset());
        for (String s : projectNameList) {
            File repoDir = new File(String.format(gitFile, s));
            String inPathStr = String.format(inPath, s);
            //首先拉取远端代码
            GitUtils.gitPull(repoDir);

            File sFile = null;
            boolean flag = false;
            List<File> allFiles = FileUtil.getAllFiles(inPathStr);
//            List<File> allFiles = FileUtil.getAllFiles(String.format(gitFile, s));
            for (File singleFile : allFiles) {
                for (String s1 : old.split(separator)) {
                    if (FileUtils.readFileToString(singleFile, Charset.defaultCharset()).contains(s1)) {
                        log.warn("需要修改的文件有：" + singleFile.getAbsolutePath());
                        String haveTheBeforeAllValueGetValue = isHaveTheBeforeAllValueGetValue(singleFile, s1, separator);
                        System.out.println(singleFile.getAbsolutePath());
                        FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_file.txt"),singleFile.getAbsolutePath()+":"+ haveTheBeforeAllValueGetValue ,Charset.defaultCharset(),true);
                        FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_file.txt"),"\r\n" ,Charset.defaultCharset(),true);
                        flag = true;
                    }
                }

            }

            if(flag){
                log.warn("需要修改的项目有：" + s);
                FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_project.txt"),s ,Charset.defaultCharset(),true);
                FileUtils.write(new File(outputDir+"/"+projectName+"_need_update_project.txt"),"\r\n" ,Charset.defaultCharset(),true);
            }


        }

    }


    public static boolean isHaveTheBeforeValue(File file, String spiltBeforeValue, String separator, String newValue) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String replace = oldValue;

        String[] splitOld = spiltBeforeValue.split(separator);
        boolean flag = false;
        for (String s1 : splitOld) {
            String tmp = replace;
            replace = replace.replaceAll(s1, newValue);
            //此处进行判断哪些文本文件是修改了的
            if (replace.contains(newValue) && tmp.contains(s1) && !replace.equals(tmp)) {
                flag = true;
            }
        }
        return flag;
    }


    public static String isHaveTheBeforeValueGetValue(File file, String spiltBeforeValue, String separator, String newValue) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String returnValue = "";

        String replace = oldValue;

        String[] splitOld = spiltBeforeValue.split(separator);
        boolean flag = false;
        for (String s1 : splitOld) {
            String tmp = replace;
            replace = replace.replaceAll(s1, newValue);
            //此处进行判断哪些文本文件是修改了的
            if (replace.contains(newValue) && tmp.contains(s1) && !replace.equals(tmp)) {
                returnValue = s1;
                flag = true;
            }
        }
        if (flag){
            return returnValue;
        }else {
            return null;
        }
    }


    public static String isHaveTheBeforeAllValueGetValue(File file, String spiltBeforeValue, String separator) throws IOException {

        String oldValue = FileUtils.readFileToString(file, Charset.defaultCharset());

        String[] splitOld = spiltBeforeValue.split(separator);

        StringBuilder returnValue = new StringBuilder();

        boolean flag = false;
        for (String s1 : splitOld) {
            if (oldValue.contains(s1)) {
                returnValue.append(s1 + ",");
                flag = true;
            }
        }
        if (flag){
            return returnValue.toString();
        }else {
            return null;
        }
    }
}
